Request = function(){
  this.urlPrefix = "http://siddur.online/traktask/";
//  this.urlPrefix = "http://siddur.cn/traktask/";
//  this.urlPrefix = "http://localhost:8080/traktask/";
};

Request.prototype.doGet = function(url, success, failure, accept){
  var xhr = new XMLHttpRequest();
  xhr.open('GET', this.urlPrefix + url);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.setRequestHeader('Accept', accept || 'application/json');
  xhr.onload = function () {
    if (this.status === 200) {
      var response = this.responseText;
      console.log(response);
      if(success){
        success(response);
      }else{
        failure(response);
      }
    }else{
      if(failure){
        failure();
      }
    }
  };
  xhr.send();
};

Request.prototype.doPost = function(url, data, success, failure){
  var xhr = new XMLHttpRequest();
  xhr.open('POST', this.urlPrefix + url);
  if(typeof data == 'object'){
    xhr.setRequestHeader('Content-Type', 'application/json');
    data = JSON.stringify(data);
  }else{
    xhr.setRequestHeader('Content-Type', 'text/plain');
  }
  xhr.setRequestHeader('Accept', 'application/json');
  xhr.onload = function () {
    if (this.status === 200) {
      var response = this.responseText;
      console.log(response);
      if(success){
        success(response);
      }else{
        failure(response);
      }
    }else{
      if(failure){
        failure();
      }
    }
  };
  xhr.send(data);
};



